

from typing import Optional

class ChainIterable:
    def __init__(self, 
                 node: "ChainIterator"):
        self.node = node
    
    def __next__(self) -> "ChainIterator":
        if not self.node:
            raise StopIteration
        
        curr_node, self.node = self.node, self.node.next
        return curr_node
    
    def __iter__(self) -> "ChainIterable":
        return self
    
class ChainIterator:
    def __init__(self, 
                 attr: dict, 
                 next: Optional["ChainIterator"]=None):
        
        self.attr = attr 
        self.next = next 
        
    def __iter__(self) -> ChainIterable:
        return ChainIterable(self)
    
    
if __name__ == '__main__':
    node1 = ChainIterator(dict(value=1))
    node2 = ChainIterator(dict(value=2))
    node3 = ChainIterator(dict(value=3))
    node1.next = node2 
    node2.next = node3 
    
    node1 = iter(node1)
    first = next(node1)
    
    for node in node1:
        print(node.attr)